package com.atguigu.distributed.lock.leecode.greedy;

/**
 * 力扣中等134加油站
 */
public class CanCompleteCircuit {
    public static void main(String[] args) {
        int[] gas  = {2,3,4};
        int[] cost = {3,4,3};
        CanCompleteCircuit test = new CanCompleteCircuit();
        int i = test.canCompleteCircuit(gas, cost);
        System.out.println("res==>"+i);
    }
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas.length != cost.length) {
            throw new RuntimeException("参数有误");
        }
        int n = gas.length;
        int i = 0;
        while (i < n) {
            int sumOfGas = 0;
            int sumOfCost = 0;
            int cnt = 0;
            while (cnt < n) {
                int j = (i + cnt) % n;
                sumOfGas += gas[j];
                sumOfCost += cost[j];
                if (sumOfCost > sumOfGas) {
                    break;
                }
                cnt++;
            }
            if (cnt == n) {
                return i;
            } else {
                i = i + cnt + 1;
            }
        }
        return -1;
    }
}
